<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>申报名称</p>
          <el-input v-model="qo.Filter" placeholder="请输入申报名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>申报状态</p>
          <el-select v-model="RequestStatusCodes" size="mini" placeholder="请选择申报状态" filterable @change="getPageList()">
            <el-option :value="null" label="全部" />
            <el-option v-for="item in dict.duanExamRequestStatus" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <p>申报时间</p>
          <el-date-picker
            v-model="chooseTime"
            size="mini"
            type="daterange"
            style="width:100%"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="handleTimeChange"
          />
        </el-col>
        <el-col :span="6" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="reset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <el-row class="gl_my_15">
      <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="Jump('add')">申报入口</el-button>
    </el-row>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="序号" type="index" :index="table_index" />
      <el-table-column label="申报名称" prop="name" :min-width="120" />
      <el-table-column label="申报时间" min-width="120">
        <template slot-scope="{row}">
          <span v-if="row.submissionTime">
            {{ row.submissionTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="申报机构" min-width="100" prop="organizationUnitName" />
      <el-table-column label="申报状态" prop="requestStatus.name" />
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="Jump('inquiry', row.id)">查看</el-button>
          <el-button size="mini" type="text" :disabled="row.requestStatusCode !== 'Draft'" @click="Jump('edit', row.id)">编辑</el-button>
          <el-button v-if="row.requestStatusCode === 'Draft'" size="mini" type="text" @click="clickDelete(row)"><span class="gl_color_error">删除</span></el-button>
          <el-button v-else size="mini" type="text" disabled @click="clickDelete(row)">删除</el-button>
          <el-button size="mini" type="text" :disabled="row.requestStatusCode !== 'Pending'" @click="clickCancel(row)">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="qo.MaxResultCount"
        :current-page="qo.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import {
  Api_GET_ClubDuanExamRequestQO // 查询级段位考试申请列表
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubDuanExamRequest, // 查询级段位考试申请列表
  api_DELETE_ClubDuanExamRequestId, // 删除指定级段位考试申请
  api_PUT_ClubDuanExamRequestIdRevoke // 撤销指定级段位考试申请
} from '@/extend/api/hallswaggerApi/app';
export default {
  data () {
    return {
      qo: new Api_GET_ClubDuanExamRequestQO(),
      tableData: [],
      loading: false,
      total: 0,
      chooseTime: [],
      RequestStatusCodes: null
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache,
      user: state => state.user.currentOU
    })
  },
  created() {
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict' }),
    getPageList(page) {
      this.qo.Page = page || 1;
      this.qo.SkipCount = (this.qo.Page - 1) * this.qo.MaxResultCount;
      this.qo.IncludeRequestStatus = true;
      this.qo.IncludeRequestOrganizationUnits = true;
      this.qo.IncludeOfficials = true;
      this.qo.OrganizationUnitId = this.user;
      this.qo.RequestStatusCodes = this.RequestStatusCodes;
      this.qo.Sorting = 'creationTime DESC';
      this.loading = true;
      api_GET_ClubDuanExamRequest(this.qo).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    reset() {
      this.qo = new Api_GET_ClubDuanExamRequestQO();
      this.chooseTime = [];
      this.RequestStatusCodes = null;
      this.getPageList();
    },
    // 激活
    clickActivation(row) {
      if (row.id) {
        this.loading = true;
        api_POST_ClubStudentCardsIdActivateStudentCard(row.id).then(res => {
          this.loading = false;
          this.$message({ type: 'success', message: '激活成功' });
          this.getPageList(this.qo.Page);
        }, error => {
          this.loading = false;
          const _msg = error.error.message || error.message;
          this.$message({ type: 'error', message: _msg });
        });
      }
    },
    // 删除
    clickDelete(row) {
      this.$confirm(`您确定要删除该项申报吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true;
        api_DELETE_ClubDuanExamRequestId(row.id).then(() => {
          this.$message({ type: 'success', message: '删除成功！' });
          this.getPageList(this.qo.Page);
        }, () => {
          this.tableLoading = false;
          this.$message({ type: 'error', message: '删除失败！' + error.error.message });
        });
      }).catch(() => {});
    },
    // 撤销
    clickCancel(row) {
      this.$confirm(`您确定要撤销该项申报吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true;
        api_PUT_ClubDuanExamRequestIdRevoke(row.id).then(() => {
          this.$message({ type: 'success', message: '撤销成功！' });
          this.getPageList(this.qo.Page);
        }, () => {
          this.tableLoading = false;
          this.$message({ type: 'error', message: '撤销失败！' + error.error.message });
        });
      }).catch(() => {});
    },
    // 申报入口的跳转
    Jump(type, id) {
      let name = '';
      if (type === 'inquiry') {
        name = 'DeclareDetail';
      } else if (type === 'edit') {
        name = 'DeclareEdit';
      } else {
        name = 'DeclareEntra';
      }
      this.$router.push({
        name: name,
        query: {
          declareId: id || ''
        }
      });
    },
    handleTimeChange(time) {
      this.qo.MinSubmissionTime = time ? time[0] + ' 00:00:00' : null;
      this.qo.MaxSubmissionTime = time ? time[1] + ' 23:59:59' : null;
      this.getPageList();
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.qo.MaxResultCount = pagesize;
      this.getPageList();
    },
    // 序号递增
    table_index(index) {
      return (this.qo.Page - 1) * this.qo.MaxResultCount + index + 1;
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
